package cn.shiro.shiroservice.common.exception;

import cn.shiro.shiroservice.common.utils.R;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.io.PrintStream;

/**
 * &#064;Time 2024 一月 星期二 16:11
 *
 * @author ShangGuan
 */
@RestControllerAdvice
public class GlobeException {

    private final Logger log = LoggerFactory.getLogger(GlobeException.class);

    @ExceptionHandler(AuthorizationException.class)
    public R<String> authorizationException(AuthorizationException authorizationException){
        String message = authorizationException.getMessage();
        log.info("message:{}",message);
        authorizationException.printStackTrace();
        return R.fail(403,"没有权限");
    }

    @ExceptionHandler(UnauthenticatedException.class)
    public R<String> unauthenticatedException(UnauthenticatedException unauthenticatedException){
        String message = unauthenticatedException.getMessage();
        log.info("message:{}",message);
        unauthenticatedException.printStackTrace();
        return R.fail(403,"登陆过期->请重新登陆");
    }


    @ExceptionHandler(AuthenticationException.class)
    public R<String> authenticationException(AuthenticationException authenticationException){
        String message = authenticationException.getMessage();
        log.info("message:{}",message);
        authenticationException.printStackTrace();
        return R.fail(403,"token验证失败");
    }

    @ExceptionHandler(Exception.class)
    public R<String> exception(Exception exception){
        String message = exception.getMessage();
        log.error("message:{}",message);
        exception.printStackTrace();
        return R.fail(500,"服务器异常");
    }


}
